package com.ks.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池任务配置类
 *
 * @author KingAusten
 * @date 2022/08/17 16:58:56
 */
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig{

    /**
     * 核心池大小
     */
    private static final int CORE_POOL_SIZE = Runtime.getRuntime()
            .availableProcessors();

    /**
     * 线程池任务执行人
     *
     * @return {@link ThreadPoolTaskExecutor }
     */
    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setQueueCapacity(CORE_POOL_SIZE << 8);
        executor.setMaxPoolSize(CORE_POOL_SIZE << 3);
        executor.setKeepAliveSeconds(30);
        executor.setThreadNamePrefix("信息发送线程池-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }

}
